package com.kira.scaffoldmvc.Controller;

import com.kira.scaffoldmvc.POJO.Annotation.Check;
import com.kira.scaffoldmvc.POJO.Result;
import com.kira.scaffoldmvc.Service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
@Slf4j
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/registry")
    public Result registry(String name, String password, @Check @Validated String account) {
        try {
            Integer registry = userService.registry(name, password, account);
            if (registry == 1)
                return Result.error("账号已被注册");

        } catch (Exception e) {
            log.error(e.toString());
            return Result.error("注册失败");
        }

        return Result.success("注册成功");
    }

    @GetMapping("/login")
    public Result login(String account, String password) {

        return userService.login(account, password);


    }


    @GetMapping("/setmanage")
    public Result setmanage(Integer right, String userId) {

        return userService.setManage(right, userId);

    }


    @GetMapping("/lock")
    public Result lock(String userId) {
        return userService.lock(userId);
    }

    @GetMapping("/unlock")
    public Result unlock(String userId) {
        return userService.unlock(userId);
    }


    @GetMapping("/getAllUser")
    public Result getAllUser(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return userService.getAllUser(pageNum, pageSize);
    }

    @GetMapping("/getUser")
    public Result getUser(String userId) {
        return userService.getUser(userId);
    }

}
